package bb.lanxing.calc.notify;

import android.content.Context;

public class SpeedAlert {
    private AlertControlDelegate alertControlDelegate;
    private boolean alertEnable;
    private double alertValue;
    private Context context;

    public SpeedAlert(Context context, int soundRawId) {
        this.context = context;
        this.alertControlDelegate = new AlertControlDelegate(context, soundRawId);
    }

    public void checkSpeed(double speed) {
        if (this.alertEnable) {
            if (speed >= this.alertValue) {
                play();
            } else {
                stop();
            }
        }
    }

    public void play() {
        AlertControlDelegate alertControlDelegate = this.alertControlDelegate;
        if (alertControlDelegate != null && !alertControlDelegate.isPlaying()) {
            this.alertControlDelegate.play();
        }
        VibratorHelper.vibrate(this.context, VibratorHelper.VIBRATE_ALERT_PATTERN, 0);
    }

    public void stop() {
        AlertControlDelegate alertControlDelegate = this.alertControlDelegate;
        if (alertControlDelegate != null && alertControlDelegate.isPlaying()) {
            this.alertControlDelegate.stop();
        }
        VibratorHelper.cancel(this.context);
    }

    public void release() {
        AlertControlDelegate alertControlDelegate = this.alertControlDelegate;
        if (alertControlDelegate != null) {
            alertControlDelegate.release();
            this.alertControlDelegate = null;
        }
        VibratorHelper.cancel(this.context);
    }

    public void setAlertEnable(boolean enable) {
        this.alertEnable = enable;
        if (enable) {
            return;
        }
        AlertControlDelegate alertControlDelegate = this.alertControlDelegate;
        if (alertControlDelegate != null && alertControlDelegate.isPlaying()) {
            this.alertControlDelegate.stop();
        }
        VibratorHelper.cancel(this.context);
    }

    public void setAlertValue(int value) {
        this.alertValue = value / 3.6d;
    }
}
